<?php
namespace Wx\Controller;
use Think\Controller;
/*
*农户
*拼团管理
*/
class FarmerSpellgroupController extends CommonController {

    public function index(){
		$shop_spellgroup=D('shop_spellgroup');
        //商品状态
        $state = $_GET['state'];
//        dump($state);
        if($state!=''){
            if($state==0){//为0说明是审核状态，包含审核失败和待审
                $map =  "(state = 0 OR state = 2)";
            }else{
                $map =  "state = ".$state;
            }//end if
        }else{
            $map = "state != 3";
        }//end if
		$shop=$shop_spellgroup->where("u_id=".session('u_id')." and ".$map)->order("id desc")->select();
		foreach($shop as $i=>$one){
			$img=explode(',',$one['h_imgs']);
			$shop[$i]['f_img']=$img[0];//第一张图片作为封面图
		}//end for
		$this->assign('shop',$shop);
        $this->assign('state',$state);
        $this->display('productmessage-nh');
    }//end fun
	//编辑
	public function edit(){
		$category=D('category');
		$type=$category->where("pid=22")->select();
		$this->assign('type',$type);
		$shop_spellgroup=D('shop_spellgroup');
		$shop=$shop_spellgroup->where("id=".$_GET['id'])->find();
        //批发价
        $shop['price_range'] = M('spellgroup_price_range')->where('sg_id='.$_GET['id'])->order('id asc')->select();
//        dump($shop['price_range']);
		//图片分拆
		//首页
		$img=explode(',',$shop['h_imgs']);
		$shop['img1']=$img[0];
		$shop['img2']=$img[1];
		//内容
		$img=explode(',',$shop['c_imgs']);
		$shop['img3']=$img[0];
		$shop['img4']=$img[1];
		$shop['img5']=$img[2];
		$shop['img6']=$img[3];
		
		if($shop['state']==2){
			//不通过编辑
			$shop['dowhat']="edit";
			
			}else{
				//结束再上架
				$shop['dowhat']="up";
				
				}//end if
		$this->assign('shop',$shop);
        $this->display('editproduct-nh');
	}//end fun
	//更新数据
	public function upd(){
		$shop_spellgroup=D('shop_spellgroup');
		$data=array();
		$data['u_id']=session('u_id');
		$data['s_id']=0;
		$data['type']=0;
		$data['title']=$_POST['title'];
		//处理多余的逗号		
		$data['h_imgs']=preg_replace('/(,)+/i',',',$_POST['h_imgs']);
		$data['h_imgs']=rtrim($data['h_imgs'],',');//去尾
		$data['h_imgs']=ltrim($data['h_imgs'],',');//去头

		$data['c_imgs']=preg_replace('/(,)+/i',',',$_POST['c_imgs']);
		$data['c_imgs']=rtrim($data['c_imgs'],',');//去尾
		$data['c_imgs']=ltrim($data['c_imgs'],',');//去头
		
		//$data['content']=$_POST[''];
		$starttime=strtotime($_POST['starttime']);
		$data['starttime']=$starttime;
		$endtime=strtotime($_POST['endtime']);
		$data['endtime']=$endtime;
		
		$data['num']=$_POST['num'];
		//$data['min_num']=$_POST[''];
		//$data['price']=$_POST[''];
		$data['u_num']=$_POST['u_num'];
		$data['unit']=$_POST['unit'];
		//$data['actual_num']=$_POST[''];
		$data['wholesale']=$_POST['wholesale'];//农户报价
		$data['server']=$_POST['server'];//拼团须知
		$data['state']=0;
		$data['uptime']=time();
		if($_POST['dowhat']=='edit'){
			//echo "编辑";
			$alt=$shop_spellgroup->where("id=".$_POST['id'])->save($data);
			if($alt){
                // 处理批发价
                if($_POST['start']!=''&&$_POST['end']!=''&&$_POST['p_price']!=''){
                    // 处理数组
                    foreach ($_POST['start'] as $k=>$v){
                        foreach ($_POST['p_id'] as $k1=>$v1){
                            $p_data[$k1]['id'] = $v1;
                        }//end foreach
                        foreach ($_POST['end'] as $k2=>$v2){
                            $p_data[$k2]['end'] = $v2;
                        }//end foreach
                        foreach ($_POST['p_price'] as $k3=>$v3){
                            $p_data[$k3]['price'] = $v3;
                        }//end foreach
                        $p_data[$k]['start'] = $v;
                    }//end foreach
                    // 修改
                    foreach ($p_data as $v){
                        M('spellgroup_price_range')->save($v);
                    }//end foreach

                }//end if
				$this->redirect('FarmerSpellgroup/index');
				}else{
					$this->error('更新失败');
				}//end if
			}else{
				//echo "上架";
				$data['addtime']=time();
				//删除原本
				$shop_spellgroup->where("id=".$_POST['id'])->setField('state',3);
				//添加新的
				$alt=$shop_spellgroup->add($data);
				if($alt){
                    // 处理批发价-如果是上架，修改商品批发价的id为新增的数据id
                    if($_POST['start']!=''&&$_POST['end']!=''&&$_POST['p_price']!=''){
                        // 处理数组
                        foreach ($_POST['start'] as $k=>$v){
                            foreach ($_POST['p_id'] as $k1=>$v1){
                                $p_data[$k1]['id'] = $v1;
                            }//end foreach
                            foreach ($_POST['end'] as $k2=>$v2){
                                $p_data[$k2]['end'] = $v2;
                            }//end foreach
                            foreach ($_POST['p_price'] as $k3=>$v3){
                                $p_data[$k3]['price'] = $v3;
                            }//end foreach
                            $p_data[$k]['start'] = $v;
                            $p_data[$k]['sg_id'] = $alt;
                        }//end foreach
                        // 修改
                        foreach ($p_data as $v){
                            M('spellgroup_price_range')->save($v);
                        }//end foreach

                    }//end if
					$this->redirect('FarmerSpellgroup/index');
					}else{
						$this->error('上架失败');
					}//end if
			}//end if
	}//end fun
	//删除
	public function del(){
		$shop_spellgroup=D('shop_spellgroup');
		$shop_spellgroup->where("id=".$_GET['id'])->setField('state',3);
	}//end fun
    public function add(){
		if($_POST['dowhat']){
			$data=array();
			$data['u_id']=session('u_id');
			$data['s_id']=0;
			$data['type']=0;
			$data['title']=$_POST['title'];
			
			//处理多余的逗号		
			$data['h_imgs']=preg_replace('/(,)+/i',',',$_POST['h_imgs']);
			$data['h_imgs']=rtrim($data['h_imgs'],',');//去尾
			$data['h_imgs']=ltrim($data['h_imgs'],',');//去头
	
			$data['c_imgs']=preg_replace('/(,)+/i',',',$_POST['c_imgs']);
			$data['c_imgs']=rtrim($data['c_imgs'],',');//去尾
			$data['c_imgs']=ltrim($data['c_imgs'],',');//去头
			
			//$data['content']=$_POST[''];
			$starttime=strtotime($_POST['starttime']);
			$data['starttime']=$starttime;
			$endtime=strtotime($_POST['endtime']);
			$data['endtime']=$endtime;
			
			$data['num']=$_POST['num'];
			//$data['min_num']=$_POST[''];
			//$data['price']=$_POST[''];
			$data['u_num']=$_POST['u_num'];
			$data['unit']=$_POST['unit'];
			//$data['actual_num']=$_POST[''];
			$data['wholesale']=$_POST['wholesale'];//农户报价
			$data['server']=$_POST['server'];//拼团须知
			$data['state']=0;
			$data['addtime']=time();
			$data['uptime']=time();
			$shop_spellgroup=D('shop_spellgroup');
			$alt=$shop_spellgroup->add($data);
			if($alt){
                // 处理批发价
                if($_POST['start']!=''&&$_POST['end']!=''&&$_POST['p_price']!=''){
                    // 处理数组
                    foreach ($_POST['start'] as $k=>$v){
                        foreach ($_POST['end'] as $k2=>$v2){
                            $p_data[$k2]['end'] = $v2;
                        }
                        foreach ($_POST['p_price'] as $k3=>$v3){
                            $p_data[$k3]['price'] = $v3;
                        }
                        $p_data[$k]['start'] = $v;
                        $p_data[$k]['sg_id'] = $alt;
                    }//end foreach
                    // 添加数据
                    foreach ($p_data as $v){
                        M('spellgroup_price_range')->add($v);
                    }
                }//end if
				$this->redirect('FarmerSpellgroup/index');

				}else{
					$this->error('发布失败');
				}//end if
			}else{
				$category=D('category');
				$type=$category->where("pid=22")->select();
				$this->assign('type',$type);
				$this->display('addproduct-nh');
		}//end if
        
    }//end fun
}